package com.enjoy.concurrent.ch5;

import org.junit.Test;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

public class LinkedBlockingDequeTest {
    // 先进后出
    @Test
    public void test1() {

        BlockingDeque<String> blockingDeque = new LinkedBlockingDeque<>();
        blockingDeque.offer("aa");
        blockingDeque.offer("bb");
        blockingDeque.offer("cc");

        System.out.println("blockingDeque.pollLast() = " + blockingDeque.pollLast());
        System.out.println("blockingDeque.pollLast() = " + blockingDeque.pollLast());
        System.out.println("blockingDeque.pollLast() = " + blockingDeque.pollLast());
    }

    // 先进先出
    @Test
    public void test2() {
        BlockingDeque<String> blockingDeque = new LinkedBlockingDeque<>();
        blockingDeque.offer("aa");
        blockingDeque.offer("bb");
        blockingDeque.offer("cc");
        System.out.println("blockingDeque.pollFirst() = " + blockingDeque.pollFirst());
        System.out.println("blockingDeque.pollFirst() = " + blockingDeque.pollFirst());
        System.out.println("blockingDeque.pollFirst() = " + blockingDeque.pollFirst());
    }

    // 先进后出
    @Test
    public void test3() {
        BlockingDeque<String> blockingDeque = new LinkedBlockingDeque<>();
        blockingDeque.offerLast("aa");
        blockingDeque.offerLast("bb");
        blockingDeque.offerLast("cc");

        System.out.println("blockingDeque.pollLast() = " + blockingDeque.pollLast());
        System.out.println("blockingDeque.pollLast() = " + blockingDeque.pollLast());
        System.out.println("blockingDeque.pollLast() = " + blockingDeque.pollLast());
    }

    // 先进先出
    @Test
    public void test4() {
        BlockingDeque<String> blockingDeque = new LinkedBlockingDeque<>();
        blockingDeque.offerLast("aa");
        blockingDeque.offerLast("bb");
        blockingDeque.offerLast("cc");

        System.out.println("blockingDeque.poll() = " + blockingDeque.poll());
        System.out.println("blockingDeque.poll() = " + blockingDeque.poll());
        System.out.println("blockingDeque.poll() = " + blockingDeque.poll());
    }

    // 查询
    @Test
    public void test5() {
        BlockingDeque<String> blockingDeque = new LinkedBlockingDeque<>();
        blockingDeque.addFirst("aa");
        blockingDeque.addFirst("bb");
        blockingDeque.addFirst("cc");

        System.out.println(blockingDeque.element());
        System.out.println(blockingDeque.peek());

        System.out.println("blockingDeque.poll() = " + blockingDeque.getFirst());
        System.out.println("blockingDeque.poll() = " + blockingDeque.getFirst());
        System.out.println("blockingDeque.poll() = " + blockingDeque.getFirst());
    }


}
